home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / libs / usergroup / getpass.c < prev    next >
C/C++ Source or Header  |  1994-02-24  |  3KB  |  119 lines

  1. RCS_ID_C="$Id: getpass.c,v 1.4 1994/01/21 08:13:47 ppessi Exp $"; 
  2. /*
  3.  * getpass() --- password checking routine
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP usergroup.library.
  8.  *
  9.  * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Sun Nov 28 17:45:55 1993 ppessi
  13.  * Last modified: Fri Jan 21 06:49:11 1994 ppessi
  14.  *
  15.  * $Log: getpass.c,v $
  16.  * Revision 1.4  1994/01/21  08:13:47  ppessi
  17.  * Updated documentation
  18.  *
  19.  * Revision 1.3  1994/01/19  10:04:03  ppessi
  20.  * Added error checking
  21.  *
  22.  * Revision 1.2  94/01/15  05:40:38  ppessi
  23.  * Hard disk crash
  24.  * 
  25.  * Revision 1.1  93/11/30  03:32:14  ppessi
  26.  * Initial revision
  27.  * 
  28.  */
  29.  
  30. #include "base.h"
  31. #include "libfunc.h"
  32.  
  33. #define EOF (-1)
  34.  
  35. /****** usergroup.library/getpass ******************************************
  36.  
  37.     NAME
  38.         getpass - get a password
  39.  
  40.     SYNOPSIS
  41.         password = getpass(prompt)
  42.            D0                A1
  43.  
  44.         char *getpass(const char *);
  45.  
  46.     FUNCTION
  47.         The getpass() function displays a prompt to, and reads in a password
  48.         from "CONSOLE:".  If this device is not accessible, getpass()
  49.         displays the prompt on the standard error output and reads from the
  50.         standard input.
  51.  
  52.         The password may be up to _PASSWORD_LEN (currently 128) characters
  53.         in length.  Any additional characters and the terminating newline
  54.         character are discarded.
  55.  
  56.         Getpass turns off character echoing while reading the password.
  57.  
  58.     RESULT
  59.         password -  a pointer to the null terminated password
  60.  
  61.     FILES
  62.         Special device "CONSOLE:"
  63.  
  64.     SEE ALSO
  65.         crypt()
  66.  
  67.     HISTORY
  68.         A getpass function appeared in Version 7 AT&T UNIX.
  69.  
  70.     BUGS
  71.         The getpass function leaves its result in an internal static object
  72.         and returns a pointer to that object.  Subsequent calls to getpass
  73.         will modify the same object.
  74.  
  75.         The calling program should zero the password as soon as possible to
  76.         avoid leaving the cleartext password visible in the memory.
  77.  
  78. ****************************************************************************
  79. */
  80.  
  81. /*
  82.  * read the password 
  83.  */
  84. SAVEDS ASM char *R_getpass(REG(a1) const char *prompt)
  85. {
  86.   BPTR conout, conin = Open("CONSOLE:", MODE_OLDFILE);
  87.   static char ch, *p, buf[_PASSWORD_LEN];
  88.   int useconsole = conin != NULL;
  89.  
  90.   /*
  91.    * read and write to CONSOLE: if possible; else read from
  92.    * stdin and write to stderr.
  93.    */
  94.   if (useconsole) {
  95.     conout = conin;
  96.   } else {
  97.     conin = Input();
  98.     conout = Output();
  99.   }
  100.   
  101.   FPuts(conout, (UBYTE *)prompt);
  102.   Flush(conout);
  103.  
  104.   SetMode(conin, 1);        /* Raw */
  105.  
  106.   for (p = buf; (ch = FGetC(conin)) != EOF && ch != '\n' && ch != '\r';)
  107.     if (p < buf + _PASSWORD_LEN)
  108.       *p++ = ch;
  109.   *p = '\0';
  110.  
  111.   Write(conout, "\n", 1);
  112.   SetMode(conin, 0);        /* Cooked */
  113.  
  114.   if (useconsole)
  115.     Close(conin);
  116.  
  117.   return buf;
  118. }
  119.